<%@page import="jpa.entities.Employee"%>
<%@page import="jsp.Datetime"%>
<%@page import="jpa.entities.Project"%>
<%@page import="org.omg.PortableInterceptor.USER_EXCEPTION"%>
<jsp:useBean id="user" class="jsp.classes.Session" scope="session"/>
<%
    String action = "";
    action = request.getParameter("action");

    String breadCrumbs = "";
    if (action != null) {
        breadCrumbs = "> " + action;
    }
%>

<script type="text/javascript">
    $("#selectProject").change(function () {
        var str = "";
        $("#selectProject option:selected").each(function () {
            str += $(this).val();
        });
        loadProjectAdmin(str);
    })
    .trigger('change');
    
    function loadProjectAdmin(id){
        $("#projectDetails").load("projectDetails.jsp?pid="+id);
    }
    
    function submitForm()
    {   
        var stateMessage = "";
        
        var data = $("#newUserForm").serialize();
        
        $.ajax({
            type: "post",
            url: "http://localhost:8080/Baalzebub/newuser",
            data: data,
            success: function (data, textStatus, jqXHR) {
                if(data == "true")
                {
                    $("#state").html("The new user is made.");
                    
                }
                else
                {
                    $("#state").html("An error has occurred");
                }
            },
            error: function () {
                $("#state").html("Please fill in all the required fields!");
            }
        });
        
    
        
    }
    
    function addTask()
    {   
        var stateMessage = "";
        
        var data = $("#newTask").serialize();
        
        $.ajax({
            type: "post",
            url: "http://localhost:8080/Baalzebub/newtask",
            data: data,
            success: function (data, textStatus, jqXHR) {
                if(data == "true")
                {
                    $("#state").html("The new task is made.");
                    
                }
                else
                {
                    $("#state").html("An error has occurred");
                }
            },
            error: function () {
                $("#state").html("Please fill in all the required fields!");
            }
        });
        
    
        
    }
    
    
    
</script>

<p class="Title">Admin <%= breadCrumbs%></p>

<div id="adminPane">

    <div class="adminMenu">
        <a href="javascript: $('#projectPane').load('admin.jsp');" class="CreateGroupButton"><div class="CreateGroupButton"><p class="okText">Overview</p></div></a>
        <a href="javascript: $('#projectPane').load('admin.jsp?action=newUser');" class="CreateGroupButton"><div class="CreateGroupButton"><p class="okText">New User</p></div></a>
        <a href="javascript: $('#projectPane').load('admin.jsp?action=newTask');" class="CreateGroupButton"><div class="CreateGroupButton"><p class="okText">New Task</p></div></a>

        <div class="projectPanel">
            Projects

            <select multiple="true" id="selectProject"  class="projectListbox">
                <%
                    for (Project p : user.getJPAConnection().getAllProjects()) {
                        out.print("<option value=\"" + p.getId() + "\">" + p.getTitle() + "</option>");
                    }
                %>
            </select>
        </div>
    </div>

    <%
        // Laat de div met de User Settins zien.
        if (action == null) {
    %>
    <div class="addUserSettings" id="projectDetails">
        Selecteer een project links in het menu.

    </div>

    <%        } // Voer deze code uit als de actie newUser is.
    else if (action != null && action.equalsIgnoreCase("newUser")) {
    %>

    <div class="addUserSettings" id="projectDetails">
        <b>Add new user</b>
        <form id="newUserForm">
            <table>
                <tbody>
                    <tr><td colspan="2"><p styl="margin:0;padding:0;" id="state"></p></td></tr>
                    <tr>
                        <td>Username:</td>
                        <td>
                            <input class="inputField" type="text" name="username" id="username" value=""/>
                        </td>
                    </tr>
                    <tr>
                        <td>First name:</td>
                        <td><input class="inputField" type="text" name="firstname" id="firstname" value=""/></td>
                    </tr>
                    <tr>
                        <td>Last name:</td>
                        <td><input class="inputField" type="text" name="lastname" id="lastname" value=""/></td>
                    </tr>
                    <tr>
                        <td>Middle names</td>
                        <td><input class="inputField" type="text" name="middlename" id="middlename" value=""/></td>
                    </tr>
                    <tr>
                        <td>Birth date</td>
                        <td><input class="inputField" type="date" name="birthdate" id="birthdate" value=""/></td>
                    </tr>
                    <tr>
                        <td>Membership date</td>
                        <td><input class="inputField" type="date" name="memdate" id="memdate" value=""/></td>
                    </tr>
                    <tr>
                        <td>Termination date</td>
                        <td><input class="inputField" type="date" name="termdate" id="termdate" value=""/></td>
                    </tr>
                    <tr>
                        <td>Town</td>
                        <td>
                            <input class="inputField" type="text" name="town" id="town" value=""/>
                        </td>
                    </tr>
                    <tr>
                        <td>Postal code</td>
                        <td>
                            <input class="inputField" type="text" name="postalcode" id="postalcode" value=""/>
                        </td>
                    </tr>
                    <tr>
                        <td>Country</td>
                        <td>
                            <input class="inputField" type="text" name="country" id="country" value=""/>
                        </td>
                    </tr>
                    <tr>
                        <td>Social security number</td>
                        <td><input class="inputField" type="number" name="socialsecurity" id="socialsecurity" value=""/></td>
                    </tr>
                    <tr>
                        <td>Phone numbers</td>
                        <td>
                            <input class="inputField" type="tel" name="phonenumber" id="phonenumber" value="" class="disposable"/>
                            <span class="illegalphonenumber"></span>
                            <br />
                            <a id="phonenumber" class="profileaddfield">+</a>
                        </td>
                    </tr>
                    <tr>
                        <td>Email addresses</td>
                        <td>
                            <input class="inputField" type="email" name="emailaddress" value="" id="emailaddress" class="disposable"/>
                            <span class="illegalemailaddress"></span>
                            <br />
                            <a id="emailaddress" class="profileaddfield">+</a>
                        </td>
                    </tr>
                    <tr>
                        <td>Websites</td>
                        <td>
                            <input class="inputField" type="url" name="webaddress" value="" id="webaddress" class="disposable"/>
                            <span class="illegalwebaddress"></span>
                            <br />
                            <a id="webaddress" class="profileaddfield">+</a>
                        </td>
                    </tr>
                    <tr>
                        <td>Password</td>
                        <td>
                            <input class="inputField" type="password" id="password" name="password"/>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <a href="javascript: submitForm()" class="okButton"><div class="okButton"><p class="okText">OK</p></div></a>
                        </td>
                    </tr>
                </tbody>
            </table>
        </form>
    </div>

    <%        } // Voer deze code uit als de actie newUser is.
    else if (action != null && action.equalsIgnoreCase("newTask")) {
    %>

    <div class="addUserSettings" id="projectDetails">
        <b>Add new Task</b>

        <form name="newTask" id="newTask">
            <table>
                <tbody>
                    <tr><td colspan="2"><p styl="margin:0;padding:0;" id="state"></p></td></tr>
                    <tr>
                        <td>Taskname:</td>
                        <td>
                            <input class="inputField" type="text" name="taskname" id="taskname" value=""/>
                        </td>
                    </tr>
                    <tr>
                        <td>deadline:</td>
                        <td>
                            <input class="inputField" type="date" name="deadline" id="deadline" value="YYYY-MM-DD"/>
                        </td>
                    </tr>
                    <tr>
                        <td>Project:</td>
                        <td>

                            <select id="selectProject" name="project">
                                <%
                                    for (Project p : user.getJPAConnection().getAllProjects()) {
                                        out.print("<option value=\"" + p.getId() + "\">" + p.getTitle() + "</option>");
                                    }
                                %>
                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>Employee:</td>
                        <td>
                            <select name="employee" id="testje">
                                <%
                                    String selected = "";
                                    for (Employee emp : user.getJPAConnection().getAllUsers()) {

                                        out.print("<option value=\"" + emp.getId() + "\"  " + selected + ">" + emp.getUserName() + "</option>");
                                    }
                                %>

                            </select>
                        </td>
                    </tr>
                    <tr>
                        <td>Importance:</td>
                        <td>
                            <input class="inputField" type="number" name="importance" id="importance" value="1" min="1" max="5"/>
                        </td>
                    </tr>
                    <tr>
                        <td>Details:</td>
                        <td>

                            <textarea class="usersListbox" rows="8" name="details"></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td>Notes:</td>
                        <td>
                            <textarea class="usersListbox" rows="8" name="notes "></textarea>
                        </td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <a href="javascript: addTask()" class="okButton"><div class="okButton"><p class="okText">ADD</p></div></a>
                        </td>
                    </tr>
                </tbody>
            </table>

        </form>

    </div>



    <%        }
    %>
</div>